package cn.studyjams.s1.contest.autohello;

import android.app.ActivityOptions;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

import cn.studyjams.s1.contest.autohello.model.User;
import cn.studyjams.s1.contest.autohello.utils.ImageLoader.BitmapUtils;

public class ProfileActivity extends BaseActivity {

    private static final String TAG = "ProfileActivity";

    private ImageView headPicIV;
    private TextView phoneNumberTV;
    private TextView emailTV;

    private User user;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profile);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            // Show the Up button in the action bar.
            actionBar.setDisplayHomeAsUpEnabled(true);
        }

        final CollapsingToolbarLayout collapsingToolbarLayout = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar);
        headPicIV = (ImageView) findViewById(R.id.profile_head_pic);
        phoneNumberTV = (TextView) findViewById(R.id.profile_phone_number);
        emailTV = (TextView) findViewById(R.id.profile_email);

        FirebaseAuth mAuth = FirebaseAuth.getInstance();
        FirebaseUser mUser = mAuth.getCurrentUser();
        showProgressDialog(getString(R.string.loading));

        FirebaseDatabase.getInstance().getReference().child("users").child(mUser.getUid()).addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                hideProgressDialog();
                user = dataSnapshot.getValue(User.class);

                collapsingToolbarLayout.setTitle(user.getUsername());
                phoneNumberTV.setText(user.getPhoneNumber().equals("") ? getResources().getString(R.string.unknow) : user.getPhoneNumber());
                emailTV.setText(user.getEmail());

                if (!user.getPhoto().equals("")) {
                    BitmapUtils bitmapUtils = new BitmapUtils();
                    bitmapUtils.disPlay(headPicIV, user.getPhoto());
                    headPicIV.setPadding(0, 0, 0, 0);
                }
            }

            @Override
            public void onCancelled(DatabaseError error) {
                // Failed to read value
                Log.e(TAG, "Failed to read value.", error.toException());
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_profile, menu);
        return true;
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.action_edit) {
            if (user != null) {
                Intent intent = new Intent(ProfileActivity.this, UpdateProfileActivity.class);
                intent.putExtra("user", user);

                if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
                    startActivity(intent);
                } else {
                    Bundle bundle = ActivityOptions
                            .makeSceneTransitionAnimation(
                                    this,
                                    headPicIV,
                                    headPicIV.getTransitionName()
                            ).toBundle();
                    startActivity(intent, bundle);
                }
            }
        }

        return super.onOptionsItemSelected(item);
    }
}
